package com.fjeld.client.gui.screens.node;

import com.fjeld.swingx.JTransparentPanel;
import com.fjeld.middleware.jaxb.V1ID;
import com.fjeld.middleware.jaxb.V1Node;
import com.fjeld.client.util.cache.ClientCacheManager;
import com.fjeld.client.gui.util.factories.LabelFactory;

import javax.swing.*;
import java.awt.*;
import java.util.Observer;
import java.util.Observable;

import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: fjelds
 * Date: Mar 21, 2010
 * Time: 11:56:50 AM
 *
 * Shows a single environment, such as Dev, Stage or Prod, and it's actions.
 */
public class EnvironmentView extends JTransparentPanel implements Observer
{
    private static final Logger logger = Logger.getLogger(EnvironmentView.class);
    private static final ClientCacheManager CCM = ClientCacheManager.getInstance();
    public static final int HEIGHT = 50;
    private static final Dimension DIMENSION = new Dimension(200, HEIGHT);
    private V1Node environmentDescriptionNode;
    private Box actionBox = Box.createVerticalBox();

    public EnvironmentView(V1ID nodeReference)
    {
        environmentDescriptionNode = CCM.getNode(nodeReference.getNodeID());
        environmentDescriptionNode.addObserver(this);

        JLabel label = LabelFactory.createHeaderLabelBold(environmentDescriptionNode.getName());
        add(label, BorderLayout.NORTH);

        JTransparentPanel p = new JTransparentPanel();
        p.add(actionBox, BorderLayout.NORTH);

        add(p, BorderLayout.CENTER);

        //setPreferredSize(DIMENSION);
        setBorder(BorderFactory.createEmptyBorder(0,0,10,0));

        updateActions();
        this.revalidate();
        this.repaint();
    }

    private void updateActions()
    {
        actionBox.removeAll();

        logger.info("Updating environment");

        for (V1ID actionRefID : environmentDescriptionNode.getV1ID())
        {
            logger.info("New action");
            actionBox.add(new ActionView(actionRefID));
        }

        actionBox.revalidate();
        actionBox.repaint();
    }

    public void update(Observable observable, Object o)
    {
        updateActions();
    }
}
